//
//  LGOIRecordViewController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/3.
//  出入量

#import "LGOIRecordViewController.h"
#import "LGMonitoringHistoryView.h"
#import "LGPatientIORecordHeader.h"
#import "LGPatientIORecordCell.h"
#import "LGPickerView.h"
@interface LGOIRecordViewController ()<UITableViewDataSource,UITableViewDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)LGMonitoringHistoryView *historyView;
/**   */
@property (nonatomic , strong)UIView *footerView;
/**   */
@property (nonatomic , copy)NSString *monthString;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , strong)NSDateFormatter *formatter1;
/**   */
@property (nonatomic , strong)NSDateFormatter *formatter2;
/**   */
@property (nonatomic , assign)BOOL firstLoad;
@end

@implementation LGOIRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.firstLoad = YES;
    self.monthString = [self.formatter2 stringFromDate:[NSDate date]];
    [self.view addSubview:self.tableView];
    self.emptyView.userInteractionEnabled = NO;
    self.emptyView.backgroundColor = [UIColor clearColor];
}

//模块被选中
-(void)viewWillAppearAction{
    if(self.firstLoad == YES){
        [self requestData];
        self.firstLoad = NO;
    }
    if (self.dataArray.count == 0) {
        [self requestData];
    }
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.Physicalsign/ioList" parameters:@{@"timeframe":self.monthString,@"patient_id":self.patientid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            NSMutableArray *tempArray = [NSMutableArray arrayWithArray:returnData];
            for (NSInteger i=0; i<tempArray.count; i++) {
                NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:tempArray[i]];
                [tempDic setValue:[self headerViewHeight:tempDic[@"iodata"]] forKey:@"contentHeight"];
                [tempArray replaceObjectAtIndex:i withObject:tempDic];
            }
            self.dataArray = tempArray;
            [self.tableView reloadData];
        }
        if(self.dataArray.count == 0){
            self.footerView.frame = CGRectMake(0, 0, Screen_W, viewPix(300));
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无记录" offset:viewPix(60)];
        }else{
            self.footerView.frame = CGRectMake(0, 0, Screen_W, viewPix(10));
        }
        self.tableView.tableFooterView = self.footerView;
    } error:^(NSError *error) {
        self.isfirst = NO;
        if(self.dataArray.count == 0){
            self.footerView.frame = CGRectMake(0, 0, Screen_W, viewPix(300));
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无记录" offset:viewPix(60)];
        }else{
            self.footerView.frame = CGRectMake(0, 0, Screen_W, viewPix(10));
        }
        self.tableView.tableFooterView = self.footerView;
    }];
}

//选择月份
-(void)showMonthPickerView{
    NSMutableArray *yearArray = [NSMutableArray array];
    NSArray *monthArray = @[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12"];
    for (NSInteger i=2020; i<2100; i++) {
        [yearArray addObject:[NSString stringWithFormat:@"%ld",i]];
    }
    LGPickerView *pickerView = [[LGPickerView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.pickArry = @[yearArray,monthArray];
    pickerView.units = @[@"年",@"月"];
    pickerView.selectStr = [self.monthString stringByReplacingOccurrencesOfString:@"-" withString:@"|"];
    [self.view.window addSubview:pickerView];
    __weak typeof(self) weakSelf = self;
    pickerView.selectPickViewResult = ^(NSArray *resultArray) {
        weakSelf.historyView.monthLabel.text = [NSString stringWithFormat:@"%@年%@月",resultArray[0],resultArray[1]];
        weakSelf.monthString = [NSString stringWithFormat:@"%@-%@",resultArray[0],resultArray[1]];
        [weakSelf requestData];
    };
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"acdata"];
    return tempArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGPatientIORecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *tempArray = self.dataArray[indexPath.section][@"acdata"];
    cell.dataDic = tempArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    NSDictionary *tempDic = self.dataArray[section];
    return viewPix(45)+[tempDic[@"contentHeight"] floatValue];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSDictionary *tempDic = self.dataArray[section];
    CGFloat height = viewPix(45)+[tempDic[@"contentHeight"] floatValue];
    LGPatientIORecordHeader *headView = [[LGPatientIORecordHeader alloc]initWithFrame:CGRectMake(0, 0, Screen_W, height)];
    headView.monthLabel.text = tempDic[@"record_date"];
    headView.dataDic = tempDic[@"iodata"];
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(15);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    LGCornerFooterView *footView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(15))];
    footView.shadowView.backgroundColor = [UIColor colorWithString:@"#F6FAFE"];
    footView.backgroundColor = [UIColor clearColor];
    footView.bottomMargin = viewPix(10);
    return footView;
}

-(NSString *)headerViewHeight:(NSDictionary *)dataDic{
    NSString *outContent = LGNSString(dataDic[@"out_content"]).length>0?LGNSString(dataDic[@"out_content"]):@"占位文字";
    NSString *inContent = LGNSString(dataDic[@"in_content"]).length>0?LGNSString(dataDic[@"in_content"]):@"占位文字";
    CGFloat height = viewPix(32);
    height += [outContent textHeightWithFont:LGFont(16) maxWidth:Screen_W-viewPix(116)];
    height +=[inContent textHeightWithFont:LGFont(16) maxWidth:Screen_W-viewPix(116)];
    return [NSString stringWithFormat:@"%f",height];
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.tableView.frame = CGRectMake(0, 0, Screen_W, self.view.bounds.size.height);
}

#pragma mark
#pragma mark ====> 创建控件

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, self.view.bounds.size.height) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGPatientIORecordCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.historyView;
        _tableView.tableFooterView = self.footerView;
    }
    return _tableView;
}

-(LGMonitoringHistoryView *)historyView{
    if(!_historyView){
        _historyView = [[LGMonitoringHistoryView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(70))];
        _historyView.monthLabel.text = [self.formatter1 stringFromDate:[NSDate date]];
        [_historyView.monthBtn addTarget:self action:@selector(showMonthPickerView) forControlEvents:UIControlEventTouchUpInside];
    }
    return _historyView;
}

-(UIView *)footerView{
    if(!_footerView){
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(300))];
        [self emptyViewWithSuperView:_footerView frame:_footerView.bounds];
    }
    return _footerView;
}

-(NSDateFormatter *)formatter1{
    if(!_formatter1){
        _formatter1 = [[NSDateFormatter alloc] init];
        _formatter1.dateFormat = @"YYYY年MM月";
    }
    return _formatter1;
}

-(NSDateFormatter *)formatter2{
    if(!_formatter2){
        _formatter2 = [[NSDateFormatter alloc] init];
        _formatter2.dateFormat = @"YYYY-MM";
    }
    return _formatter2;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
